/**
 * @file cc1_det.c
 * @brief 
 * @author WebbSu (sumg@bjbtech.com.cn)
 * @version 1.0
 * @date 2023-12-07
 * 
 * @copyright Copyright (c) 2023  必捷必（吉林省）有限公司
 * 
 * @par 修改日志: 
 * fix  : 
 * feat : 
 */
#define LOG_TAG "cc1"
#include "cc1_det.h"
#include "../drivers/adc.h"
#include "device.h"
#include "elog.h"

#include "stm32mp1xx_hal.h"
#include "stm32mp1xx_hal_adc.h"

extern rt_adc_device_t adc;
static int cc1_state;

/**
 * @brief  CC1信号采集
 * @return int 
 */
int cc1_det(void)
{
    int ret = 0;
    int vol = 0;
    static int ch_i = 0;
    static uint32_t value = 0;

    if (ch_i >= 10)
    {
        ch_i = 0;
        value /= 10;
        vol = value * CC1_REFER_VOLTAGE / CC1_CONVERT_BITS * 10;    /* 单位mv */
        // log_d("cc1 vol %d", vol);
        if (vol > (CC1_4V_VOL - CC1_VOL_OFFSET) && vol <= (CC1_4V_VOL + CC1_VOL_OFFSET)) 
        {
            log_d("cc1 ready.");
            cc1_state = CC1_4V_STATE;
        }
        else if (vol > (CC1_6V_VOL - CC1_VOL_OFFSET) && vol <= (CC1_6V_VOL + CC1_VOL_OFFSET))
        {
            log_d("cc1 6V.");
            cc1_state = CC1_6V_STATE;
        }
        else
        {
            log_d("cc1 12V.");
            cc1_state = CC1_12V_STATE;
        }
        value = 0;
    }
    else
    {
        ch_i++;
        rt_adc_enable(adc, CC1_CHANNEL);
        value += rt_adc_read(adc, CC1_CHANNEL);
    }
    return ret;
}

/**
 * @brief Get the cc1 state object
 * @return int 
 */
int get_cc1_state(void) {
    int ret = 0;
    ret = cc1_state;
    return ret;
}


